<?php
class AppController extends Controller {
  public $helpers = array(
    'Html', 'Javascript', 'Form', 'Time', 'Session'
  );

  public $components = array(
    'Auth', 'Session', 'Cookie'
  );

  public function beforeFilter() {
    $controller = $this->params['controller'];
    $action = $this->params['action'];
    $this->set('controller', $controller);
    $this->set('action', $action);
    $url = $this->params['url']['url'];
    if ( substr($url, 0, 1) != '/' ) {
      $url = '/' . $url;
    }
    $current_path = $this->webroot . $url;
    $current_path = str_replace('//', '/', $current_path);
    Configure::write('infojabotabek.current_path', $current_path);

    $current_url = Configure::read('infojabotabek.site_domain') . $current_path;
    $current_url = str_replace('//', '/', $current_url);
    Configure::write('infojabotabek.current_url', $current_url);

    // load messages
    $this->msg = Configure::read('infojabotabek.messages');

    if ( !$this->Cookie->read('Previous.url') ) {
      $this->Cookie->write('Previous.url', $url);
      $this->Cookie->write('Previous.hoop', 0);
    } else {
      $lastUrl = $this->Cookie->read('Last.url');
      if ($lastUrl != $url) {
        $hoop = $this->Cookie->read('Previous.hoop') + 1;
        $this->Cookie->write('Previous.hoop', $hoop);
        $this->Cookie->write('Previous.url', $lastUrl);
      } else {
        $this->Cookie->delete('Previous.hoop', 0);
        $this->Cookie->write('Previous.url', $lastUrl);
      }
    }
    $this->Cookie->write('Last.url', $url);

    $this->Auth->authorize = 'controller';
    $this->Auth->loginError = 'Username/password salah';
    $this->Auth->authError = 'Anda perlu login';

    $showLoginOnTop = true;
    $showRegisterOnTop = true;
    if ( $controller == 'users' ) {
      $showLoginOnTop = false;
      $showRegisterOnTop = false;
    }

    $showProfile = false;
    $profile = array();
    $isAdmin = false;
    if ( $this->Auth->user('id') ) {
      $showLoginOnTop = false;
      $showRegisterOnTop = false;

      $showProfile = true;
      $profile = $this->Auth->user();

      if ( $this->Auth->user('type') == 'admin' ) {
          $isAdmin = true;
          $this->Auth->allow('*');
      }
    }

    $this->set(compact('showLoginOnTop', 'showRegisterOnTop', 'showProfile',
        'profile', 'isAdmin'));
  }

  public function beforeRender() {
    $this->ij = Configure::read('infojabotabek');
    $this->set('infojabotabek', Configure::read('infojabotabek'));
  }

  protected function _resizeImage($filename) {
    $pathImage = Configure::read('infojabotabek.images_path');
    $resizedPrefix = Configure::read('infojabotabek.images_resized_prefix');

    $image_info = getimagesize($pathImage . $filename);
    $image_type = $image_info[2];

    // create tmp image
    if ( $image_type == IMAGETYPE_JPEG ) {
      $image_tmp = imagecreatefromjpeg($pathImage . $filename);
    } else if ( $image_type == IMAGETYPE_GIF ) {
      $image_tmp = imagecreatefromgif($pathImage . $filename);
    } else if ( $image_type == IMAGETYPE_PNG ) {
      $image_tmp = imagecreatefrompng($pathImage . $filename);
    } else {
      $this->errorUpload = 'Unsupported image type';

      return false;
    }

    $width = imagesx($image_tmp);
    $height = imagesy($image_tmp);

    // scaling based on width
    if ( $width >= 1500 ) {
      $scale = 25;
    } else if ( $width < 1500 ) {
      $scale = 50;
    } else {
      $this->errorUpload = 'Image is too large';   
      return false;
    }

    // resizing
    $resized_width = $width * $scale / 100; 
    $resized_height = $height * $scale / 100;
    $image_resized =  imagecreatetruecolor($resized_width, $resized_height);
    imagecopyresampled(
      $image_resized, $image_tmp, 0, 0, 0, 0, $resized_width, $resized_height,
      $width, $height
    );

    // save the resized image
    if ( $image_type == IMAGETYPE_JPEG ) {
      imagejpeg($image_resized, $pathImage . $resizedPrefix .
                $filename, 75);
    } else if ( $image_type == IMAGETYPE_GIF ) {
      imagegif($image_resized, $pathImage . $resizedPrefix .
               $filename);
    } else if ( $image_type == IMAGETYPE_PNG ) {
      imagepng($image_resized, $pathImage . $resizedPrefix .
               $filename);
    }

    // create small thumb
    $this->_createThumb($image_tmp, $image_type, $filename);

    return true;
  }

  protected function _createThumb($res, $image_type, $filename) {
    $pathImage = Configure::read('infojabotabek.images_path');
    $thumbPrefix = Configure::read('infojabotabek.images_thumb_prefix');

    $width = imagesx($res);
    $height = imagesy($res);

    $long = ($width > $height) ? $width : $height;
    $crop = $long / 2;
    $x = ($width - $crop) / 2;
    $y = ($height - $crop) / 2;

    $thumb = imagecreatetruecolor(220,110);
    imagecopyresampled($thumb, $res, 0, 0, $x, $y, 220, 110, $crop, $crop);

    // save 25x25 thumb
    if ( $image_type == IMAGETYPE_JPEG ) {
      imagejpeg($thumb, $pathImage . $thumbPrefix . $filename, 75);
    } else if ( $image_type == IMAGETYPE_GIF ) {
      imagegif($thumb, $pathImage . $thumbPrefix . $filename);
    } else if ( $image_type == IMAGETYPE_PNG ) {
      imagepng($thumb, $pathImage . $thumbPrefix . $filename);
    }
  }
}
